//
//  SnapView.m
//  Weibbo
//
//  Created by 李林刚 on 13-9-8.
//  Copyright (c) 2013年 Omnivores. All rights reserved.
//

#import "SnapView.h"
#import "PreViewCell.h"
#import "UIViewAdditions.h"
@implementation SnapView
@synthesize myTableView;
@synthesize entity = _entity;

#define HeadHieght       222

+ (UIImage *) imageWithEntity:(DraftEntity *)entity
{
    SnapView  *view = [[SnapView alloc] initWithFrame:CGRectZero];
    [view setOrigin:CGPointMake(-5000, -500000)];
    [view setEntity:entity];
    
    [[[[UIApplication sharedApplication] delegate] window] addSubview:view];
    
    UIGraphicsBeginImageContext(view.bounds.size);
    [view.myTableView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [view removeFromSuperview];
    [view release];
    return img;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 20) style:UITableViewStylePlain];
        [myTableView setBackgroundColor:UIClearColor];
        [myTableView setBackgroundView:nil];
        headView=[[PreHeadView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 222)];
        myTableView.tableHeaderView = headView;
        myTableView.dataSource = self;
        myTableView.delegate = self;
        [myTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        [self addSubview:myTableView];
    }
    return self;
}

- (void)setEntity:(DraftEntity *)entity
{
    [_entity release];
    _entity = [entity retain];
    float height = 0;
    for(int i = 0; i < [_entity count];i++)
    {
        height +=  [PreViewCell heightForItem:[self.entity.draftList objectAtIndex:i] IsExpand:YES];
    }
    height +=HeadHieght;
    [headView setDraftEntity:entity];
    [self setHeight:height];
    [self setWidth:SCREEN_WIDTH];
    [myTableView setWidth:SCREEN_WIDTH];
    [myTableView setHeight:height];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.entity count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        static NSString *preViewCellIdentifier = @"PreViewCell";
        PreViewCell *preViewCell = [tableView dequeueReusableCellWithIdentifier:preViewCellIdentifier];
        if (preViewCell == nil)
        {
            preViewCell = [[[PreViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:preViewCellIdentifier]autorelease];
        }
        [preViewCell setDraftItemInfo:[self.entity.draftList objectAtIndex:indexPath.row] IsExpand:YES];
        return preViewCell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
        return [PreViewCell heightForItem:[self.entity.draftList objectAtIndex:indexPath.row] IsExpand:YES];
}

- (void)dealloc
{
    [myTableView release];
    [headView release];
    [super dealloc];
}

@end
